package top.fengleifeng.question;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : fengleifeng
 * @Package top.fengleifeng.question
 * @Description:
 * @date Date : 2020年11月23日 2:16 下午
 */
public class Num1410HTML实体解析器 {

    class Solution {

        public String entityParser(String text) {
            Map<String, String> map = getMap();
            char[] charArr = text.toCharArray();
            StringBuffer sb = new StringBuffer("");
            int pos = 0;
            int left = 0;
            int right = 0;
            int cLeft = -1;
            int cright = -1;
            while (pos < text.length()) {
                char nowChar = charArr[pos];
                if (nowChar == '&') {
                    right = pos - 1;
                    if (left != -1 && right != -1) {
                        sb.append(text, left, right + 1);
                    }
                    cLeft = pos;
                } else if (nowChar == ';') {
                    if (cLeft != -1) {
                        cright = pos;
                        String str = text.substring(cLeft, cright + 1);
                        if (map.containsKey(str)) {
                            sb.append(map.get(str));
                        } else {
                            sb.append(str);
                        }
                        left = pos + 1;
                        cLeft = -1;
                    }
                }
                pos++;
            }
            if (left != -1 ) {
                sb.append(text, left, text.length());
            }
            return sb.toString();
        }

        public Map<String, String> getMap() {
            Map<String, String> result = new HashMap();
            result.put("&quot;", "\"");
            result.put("&apos;", "\'");
            result.put("&amp;", "&");
            result.put("&gt;", ">");
            result.put("&lt;", "<");
            result.put("&frasl;", "/");
            return result;
        }
    }

    public static void main(String[] args) {
//        String text = "&amp; is an HTML entity but &ambassador; is not.";
//        String text = "and I quote: &quot;...&quot;";
//        String text = "&amp;amp;";
//        String s = new Num1410HTML实体解析器().new Solution().entityParser(text);
//        System.out.println(s);
        Long l = 123L;
        String s= Long.toString(l);
    }

}
